/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.management;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.util.ImageUtils;

import java.util.List;

public class ManageTeamAdapter extends RecyclerView.Adapter<ManageTeamAdapter.ContentViewHolder> {

    private List<DepartmentBean> teamLists;
    private OnItemClickListener mItemClickListener;
    private Context mContext;

    public ManageTeamAdapter(Context context, List<DepartmentBean> operateLists) {
        this.mContext = context;
        this.teamLists = operateLists;
    }

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_team_content, parent, false);
        return new ContentViewHolder(sView, viewType);
    }

    @Override
    public void onBindViewHolder(ContentViewHolder holder, int position) {
        int tag = holder.getTAG();
        if (tag == 0) {
            holder.fmLogoCard.setVisibility(View.GONE);
            holder.tvTeamName.setText(mContext.getResources().getText(R.string.show_my_team));
            holder.tvTeamName.getPaint().setFakeBoldText(true);
            holder.tvTeamDescribe.setText("");
        } else {
            holder.fmLogoCard.setVisibility(View.VISIBLE);
            ImageUtils.glideThumbViewByUrl(mContext,holder.mvTeamLogo,teamLists.get(position).getDepartmentLogUrl());
            holder.tvTeamName.setText(teamLists.get(position).getDepartmentName());
            holder.tvTeamDescribe.setText(String.format(mContext.getResources().getString(R.string.has_people), teamLists.get(position).getDepartmentMember().size()));
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return 0;
        return 1;
    }

    @Override
    public int getItemCount() {
        return teamLists.size();
    }


    public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTeamName, tvTeamDescribe;
        ImageView mvTeamLogo;
        ViewGroup fmLogoCard;

        private int TAG = -1;

        public ContentViewHolder(View view, int viewType) {
            super(view);
            TAG = viewType;
            tvTeamName = (TextView) view.findViewById(R.id.team_name);
            tvTeamDescribe = (TextView) view.findViewById(R.id.team_describe);
            mvTeamLogo=(ImageView) view.findViewById(R.id.team_logo);
            fmLogoCard=(ViewGroup) view.findViewById(R.id.team_logo_card);
            if (viewType != 0)
                view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition() - 1);
            }
        }

        public int getTAG() {
            return TAG;
        }

        public void setTAG(int TAG) {
            this.TAG = TAG;
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
